package onlineclass.service;



import onlineclass.dao.OResourceMapper;
import onlineclass.dao.OVideoMapper;
import onlineclass.entity.OResource;
import onlineclass.entity.OVideo;
import onlineclass.util.MyBatisUtil;
import onlineclass.util.AjaxPage;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class OResourceService {
    private static Logger Log = LoggerFactory.getLogger(OResourceService.class);
    public static final OResourceService INST = new OResourceService();
    public OResource get(long id){
        SqlSession s = MyBatisUtil.get();
        OResourceMapper mapper = s.getMapper(OResourceMapper.class);
        try {
            return  mapper.get(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }finally {
            MyBatisUtil.close(s);
        }
    }
    public AjaxPage search(int pageNo, int pageSize,String title){
        SqlSession s = MyBatisUtil.get();
        OResourceMapper mapper = s.getMapper(OResourceMapper.class);
        try {
            List<OResource> resource = mapper.search(pageNo,10,title);
            int total = mapper.searchTotal(title);
            return  new AjaxPage(resource,total,pageSize,pageNo);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }finally {
            MyBatisUtil.close(s);
        }
    }
    public int update(OResource p){
        SqlSession s = MyBatisUtil.get();
        OResourceMapper mapper = s.getMapper(OResourceMapper.class);
        try {
            return  mapper.update(p);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return -1;
        }finally {
            MyBatisUtil.close(s);
        }
    }
    public int add(OResource a){
        SqlSession s= MyBatisUtil.get();
        try {
            OResourceMapper mapper = s.getMapper(OResourceMapper.class);
            return mapper.add(a);
        }finally {
            MyBatisUtil.close(s);
        }
    }
    public int del(long id){
        SqlSession s = MyBatisUtil.get();
        OResourceMapper mapper = s.getMapper(OResourceMapper.class);
        try {
            return  mapper.del(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return -1;
        }finally {
            MyBatisUtil.close(s);
        }
    }
}
